<?php
/**
 * 员工管理
*/
apf_require_class("Bll_BLL");
apf_require_class("Bll_Hr_UserRelation");
apf_require_class("Bll_Hr_Position");
apf_require_class("Bll_Hr_Job");
apf_require_class("Bll_Hr_Department");
apf_require_class("Bll_Hr_UnusualLog");
apf_require_class("Bll_Hr_Property");
apf_require_class('Const_User');
apf_require_class('Const_BaseInfo');
apf_require_class('Const_Work');
class Bll_Hr_User extends BLL{
    private $_relation_bll =null;
    //部门bll
    private $_dep_bll = null;
    //Job bll
    private $_job_bll = null;
    //user bll
    private $_user_bll = null;

    private function get_dep_bll(){
        if(!isset($this->_dep_bll)){
            $this->_dep_bll = new Bll_Hr_Department();
        }
        return $this->_dep_bll;
    }
    private function get_job_bll(){
        if(!isset($this->_job_bll)){
            $this->_job_bll = new Bll_Hr_Job();
        }
        return $this->_job_bll;
    }
    private function get_user_bll(){
        if(!isset($this->_user_bll)){
            $this->_user_bll = new Bll_Hr_User();
        }
        return $this->_user_bll;
    }
    private function get_relation_bll(){
        if(!isset($this->_relation_bll)){
            $this->_relation_bll = new Bll_Hr_UserRelation();
        }
        return $this->_relation_bll;
    }
    /**
     *
     * @param sting $uid 可接收用户id或工号
     * @return array
     */
    public function get_user_by_id($uid){
        $result = array();
        if(empty($uid)){
            return $result;
        }
        if(is_numeric($uid)){
            $result = $this->get_user_dao()->find_by_id($uid);
        }else{
            $_where = array(
                    'user_code'=>$uid
                    );
            $result = $this->get_user_dao()->find_row($_where);
        }

        return $result;
    }
    /**
     *
     * @param array $uids
     */
    public function get_user_by_ids($uids){
        $result = array();
        if(empty($uids)){
            return $result;
        }
        $uids = array_unique($uids);
        $result = $this->get_user_dao()->find_by_ids($uids);
        return $result;
    }
    /**
     * 根据工号获取用户信息
     * @param array $user_code
     */
    public function get_user_by_code($user_code){
        $result = array();
        if(empty($user_code)){
            return $result;
        }
        $result = $this->get_user_dao()->find_row(array('user_code'=>$user_code));
        return $result;
    }
    /**
     * 根据工号获取用户信息
     * @param array $user_code
     */
    public function get_user_by_codes($user_codes){
        if(is_array($user_codes) && !empty($user_codes)){
            $where = array("user_code in ('".join("','",$user_codes)."') and ?"=>1);
            $result = $this->get_user_dao()->find($where,'',3000);
            return $result;
        }else{
            return $this->get_user_by_code($user_codes);
        }
    }

    /**
     * 添加新员工
     * @param array $data
     * @return number
     */
    public function add_user($data){
        unset($data['user_id']);
        $data['created'] = intval(time());
        $data['last_update'] = date('Y-m-d H:i:s');
        $data['reason_id'] =0;
        $data['other_reason'] ='';
        $data['leaving_date'] =0;
        $data['last_date'] =0;
        //重新生成工号
        $data['user_code'] = $this->get_user_code($data['user_type'], $data['city_id'],false);
        return $this->get_user_dao()->insert($data);
    }
    /**
     * portal 中同步的user
     */
    public function add_user_from_portal($data){
        return $this->get_user_dao()->insert($data);
    }
    /**
     * portal 中同步的user
     */
    public function update_user_from_portal($uid,$data){
        if(!empty($data['p_level'])){
            $data['p_level'] = $data['p_level'];
            $data['m_level'] = 0;
        }
        if(!empty($data['m_level'])){
            $data['m_level'] = $data['m_level'];
            $data['p_level'] = 0;
        }
        return $this->get_user_dao()->update_by_id($uid,$data);
    }
    /**
     * portal 中同步的work
     */
    public function add_work_from_portal($data){
       $this->get_work_dao()->insert($data);
       if(!isset($data['is_deleted'])){
           $this->get_relation_bll()->update_dep_position($data['user_id'], 1);//加1
       }
    }
    /**
     * portal 中同步的work
     */
    public function delete_work_from_portal($data,$where){
        $this->get_work_dao()->update($data,$where);
        if(isset($data['is_deleted'])){
            $this->get_relation_bll()->update_dep_position($data['user_id'], 0);//加1
        }
    }
    /**
     * 添加用户任职信息
     * @param unknown_type $user_id
     * @param unknown_type $job_id
     * @param unknown_type $work_time
     * @param unknown_type $is_parttime
     * @return multitype:
     */
    public function add_user_work($user_id, $job_id, $work_time, $is_parttime = 1){
        $retrun = array();
        if(empty($user_id) || empty($job_id) || empty($work_time)){
            return $retrun;
        }

        $data = array(
                'user_id' => intval($user_id),
                'job_id'  => intval($job_id),
                'work_time' => intval($work_time),
                'is_parttime' => intval($is_parttime),
                'created' => time()
        );
        $return = $this->get_work_dao()->insert($data);
        if($return){
            $this->get_relation_bll()->update_dep_position($user_id, 1);//加1
        }else{
            $return = false;
        }
        return $return;
    }
    /**
     * 删除用户任职信息
     * @param 工作ID $work_id
     */
    public function delete_user_work($user_id,$work_id){
        $retrun = array();
        if(empty($work_id) ){
            return $retrun;
        }
        $this->get_relation_bll()->update_dep_position($user_id, 0);//减少1
        $return = $this->get_work_dao()->update_by_id(intval($work_id),array('is_deleted'=>2));
        return $return;
    }
    /**
     * 获取用户工作职位信息
     * @param int $user_id
     */
    public function get_user_work($user_id, $is_parttime=1, $is_deleted=1){
        return $this->get_work_dao()->find_row(array('user_id' => intval($user_id), 'is_parttime'=>intval($is_parttime), 'is_deleted'=>intval($is_deleted)));
    }
    /**
     *获取用户工作职位信息
     */
    public function get_user_work_dep($user_id, $is_parttime=1, $is_deleted=1){
        $data = array();
        $user_work = $this->get_user_work($user_id, $is_parttime, $is_deleted);
        if(empty($user_work)){
            return $data;
        }
        $job_id=$user_work['job_id'];
        $job_bll = new Bll_Hr_Job();
        $job_info = $job_bll->get_job_dep_info($job_id);

        $bll = new Bll_Hr_Property();
        $business = $bll->get_business_list();
        $citylist = $bll->get_sort_city_list();
        $job_info['bu_name'] = $business[$job_info['bu_id']]['bu_name'];
        $job_info['city_name'] = $citylist[$job_info['city_id']];
        return $job_info;
    }
    /**
     * 更新员工
     * @param int $uid
     * @param array $data
     */
    public function update_user($uid,$data){
        if(!$uid){
            return array();
        }
        if(isset($data['user_id'])){
            unset($data['user_id']);
        }
        $data['last_update'] = date('Y-m-d H:i:s');
        return $this->get_user_dao()->update_by_id($uid,$data);
    }
    /**
     * 员人离职
     * @param int $uid
     * @param array $data
     */
    public function dimission_user($uid, $data){
        if(empty($uid) || empty($data['leaving_date']) || empty($data['last_date']) || empty($data['leaving_type'])){
            return false;
        }
        $this->update_leving_status($uid, $data['leaving_date'], $data['last_date'], $data['leaving_type'],$data['reason_id'], $data['other_reason']);
        //更新部门和职位的在职人数
        $this->get_relation_bll()->update_dep_position($uid,0);
        //取出职位信息
        $work_info = $this->get_user_work($uid);

        //清除员工职位信息
        $this->get_work_dao()->update(array('is_deleted'=>2),array('user_id'=>$uid));

        //取出汇报信息
        $parent_id=$parent_job_id=0;
        $parent_data = $this->get_relation_bll()->get_user_parent($uid,$work_info['job_id']);
        if(!empty($parent_data)){
            $parent_id = $parent_data[0]['parent_id'];
            $parent_job_id = $parent_data[0]['parent_job_id'];
        }
        //清理员工上级关系
        $this->get_relation_bll()->delete_user_report_by_uid($uid);

        //写入异动日志
        $log = new Bll_Hr_UnusualLog();
        $log->save_unusual_log($uid,$work_info['job_id'],$parent_id,$parent_job_id,3);
        return true;
    }

    /**
     * 搜索离职用户 ,按最后工作日,need multipage
     * @param int $start 20121101
     * @param int $end
     */
    public function get_users_leave_work($start, $end ,$city_ids){
        $data = array();
        if($end < $start){
            return $data;
        }
        $where =array(
                "last_date >={$start} AND last_date <= {$end} AND 1=?"=>1
        );
        $where['city_id in ('.join(',',$city_ids).') AND 1=?'] = 1;
        $data = $this->get_user_dao()->find_short_assoc($where,'',0);
        if(empty($data)){
            return $data;
        }
        //获取职位信息
        $uids = array_keys($data);

        $t_jobids = $this->_get_user_last_work($uids);
        $jobids = array_values($t_jobids);
        $jobids = array_unique($jobids);
        //离职时，职位关系已解除
        $job_list = $this->get_job_bll()->find_by_job_ids($jobids,3);
        foreach($job_list as $val){
            $t_depids[$val['job_id']]=$val['department_id'];
        }
        //部门
        $depids = array_values($t_depids);
        $depids = array_unique($depids);
        $dep_list = $this->get_dep_bll()->get_department_by_ids($depids);

        $bll = new Bll_Hr_Property();
        $business = $bll->get_business_list();
        $citylist = $bll->get_sort_city_list();
        $leave_list = $bll->get_dimission_reasion();
        $act_reason = $bll->get_active_reason();
        $passive_reason = $bll->get_passive_reason();
        foreach($passive_reason as $k=>$val){
            $act_reason[$k] = $val;
        }

        foreach($data as $user_id=>$val){
            $user_job_id = $t_jobids[$user_id];
            $dep_id = $t_depids[$user_job_id];
            $val['job_info'] = $job_list[$user_job_id];
            $val['dep_info'] = $dep_list[$dep_id];
            $val['city_name'] = $citylist[$val['work_city_id']];
            $val['bu_name'] = $business[$dep_list[$dep_id]['bu_id']]['bu_name'];
            $val['leave_type_name'] = $leave_list[$val['leaving_type']];
            $val['reason_note'] = $val['other_reason'];
            $val['reason_info'] = empty($act_reason[$val['reason_id']]) ? '' : $act_reason[$val['reason_id']];
            if($val['reason_id']==14){
                $val['reason_note'] = $val['other_reason'];
            }
            $data[$user_id]= $val;
        }
        return $data;
    }
    /**
     * 获取员工最后的工作岗位
     * @param array $uids
     * @return unknown
     */
    private function _get_user_last_work($uids){
        $where =array(
                'is_deleted'=>2,
                "user_id IN(".implode(',',$uids).") AND 1=?"=>1
        );
        $data = $this->get_work_dao()->find_short_assoc($where,'work_id DESC',0);
        foreach($data as $val){
            if(empty($t_jobids[$val['user_id']])){
                $t_jobids[$val['user_id']]=$val['job_id'];
            }
        }
        return $t_jobids;

    }
    /**
     * 某段时间内的入职用户
     * @param int $start 20120101
     * @param int $end 20120102
     */
    public function get_users_date_entry_work($start,$end,$city_ids){
        $data = array();
        if($end < $start){
            return $data;
        }
        $where =array(
                "enter_date >={$start} AND enter_date <= {$end} AND 1=?"=>1
        );
        $where['city_id in ('.join(',',$city_ids).') AND 1=?'] = 1;
        $data = $this->get_user_dao()->find_short_assoc($where,'',0);
        if(empty($data)){
            return $data;
        }
        //离职用户过滤
        foreach ($data as $k=>$v){
            if($v['job_status'] == Const_User::USER_WORK_NO){
                unset($data[$k]);
            }
        }
        //获取职位信息
        $uids = array_keys($data);
        $works = $this->get_relation_bll()->get_job_by_uids($uids, Const_Work::WORK_MAIN, Const_Util::IS_DELETE_NO);
        $job_ids = array();
        $work_time = array();//担任本职时间
        foreach($works as $val){
            $job_ids[$val['user_id']]=$val['job_id'];
            $work_time[$val['user_id']]=date('Y-m-d',strtotime($val['work_time']));
        }
        $job_ids_unique = array_unique($job_ids);
        $jobs_list = $this->get_job_bll()->find_by_job_ids($job_ids_unique, Const_Util::IS_DELETE_NO);

        //部门
        $department_ids  = array();
        foreach($jobs_list as $val){
            $department_ids[$val['job_id']] = $val['department_id'];
        }
        $department_ids_unique = array_unique($department_ids);
        $department_list = $this->get_dep_bll()->get_department_by_ids($department_ids_unique);


        $bll = new Bll_Hr_Property();
        $business = $bll->get_business_list();//事业部
        $citylist = $bll->get_sort_city_list();//城市
        $companys = $bll->get_agree_company_list();//公司
        $user_types = $bll->get_all_user_type();//员工类型
        $all_degree = Const_User::get_all_degrees();
        $p_levels = $bll->get_p_degree();
        $m_levels = $bll->get_m_degree();
        foreach($data as $user_id=>$val){
            $user_job_id = $job_ids[$user_id];
            $user_dep_id = $department_ids[$user_job_id];
            $department  = $department_list[$user_dep_id];
            $val['position_name'] = $jobs_list[$user_job_id]['position_name'];
            $val['start_job_time'] = $this->_format_date($work_time[$user_id]);//担任本职时间
            $val['department_name'] = $department['department_name'];
            $val['depart_city_name']= $citylist[$department['city_id']];//部门所在城市
            $val['cost_city_name'] = $citylist[$department['cost_city']];//成本计入城市
            $val['work_city_name'] = $citylist[$val['work_city_id']];//工作所在城市
            $val['bu_name'] = $business[$department['bu_id']]['bu_name'];
            $val['company_name'] = $companys[$val['company_id']]['company_name'];//签约公司
            $val['user_type_name'] = $user_types[$val['user_type']]['type_name'];
            $val['degree_name'] = $all_degree[$val['degree']];
            $val['enter_date'] = $this->_format_date($val['enter_date']);
            $val['birthday']   = $this->_format_date($val['birthday']);
            $val['indue_date'] = $this->_format_date($val['indue_date']);
            $val['p_m_level']  = $val['m_level']?$m_levels[$val['m_level']]['degree']:$p_levels[$val['p_level']]['degree'];
            $data[$user_id]= $val;
        }
        unset($work_time);
        unset($works);
        unset($jobs_list);
        unset($department_list);
        unset($business);
        unset($citylist);
        unset($job_ids);
        return $data;
    }
    private function _format_date($date){
        if($date){
            $date = date('Y-m-d',strtotime($date));
        }
        return $date;
    }
    /**
     * 某段时间内的离职用户
     * @param string $start 2012-01-01
     * @param string $end 2012-01-02
     */
    public function get_users_date_leave_work($start,$end){

    }
    /**
     * 员工搜索，支持员工姓名和员工编号
     * @param string $name
     * @author andycao
     */
    public function get_search_user_list($name){
        $data = array();
        if(empty($name)){
            return $data;
        }
        $sql = "SELECT user_id,user_name,city_id,work_city_id,user_code,job_status,user_type,p_level,m_level,enter_date FROM  user WHERE  ";

        $name_sql = $sql." user_name LIKE '{$name}%' AND job_status=1";
        $tmp_name_data = $this->get_user_dao()->execute($name_sql,array());

        $code_sql = $sql." user_code LIKE '{$name}%' AND job_status=1";
        $tmp_code_data = $this->get_user_dao()->execute($code_sql,array());

        if(empty($tmp_name_data) && empty($tmp_code_data)){
            return $data;
        }
        $tmp_user_data = array_merge($tmp_name_data,$tmp_code_data);

        $uids=array();
        $user_data= array();
        foreach($tmp_user_data as $val){
            $user_data[$val['user_id']] = $val;
        }
        $uids = array_keys($user_data);
        //取员工所有职位
        $tmp_job_list = $this->get_relation_bll()->get_job_by_uids($uids,0,1);
        //一个人存在多个职位
        foreach($tmp_job_list as $val){
            $data[$val['job_id']] = $val['user_id'];
        }
        $job_ids = array_keys($data);
        $job_ids = array_unique($job_ids);
        $job_list = $this->get_job_bll()->find_by_job_ids($job_ids);
        //department info
        $tmp_depids = array();
        foreach($job_list as $val){
            $tmp_depids[$val['job_id']] = $val['department_id'];
        }
        $depids = array_values($tmp_depids);
        $depids = array_unique($depids);
        $dep_bll = new Bll_Hr_Department();
        $dep_data = $dep_bll->get_department_by_ids($depids);

        $bll = new Bll_Hr_Property();
        $business = $bll->get_business_list();
        $citylist = $bll->get_sort_city_list();

        foreach($data as $job_id=>$user_id){
            $userinfo = $user_data[$user_id];
            $depid = $tmp_depids[$job_id];
            $userinfo['position_name'] = $job_list[$job_id]['position_name'];
            $userinfo['department_id'] = $depid;
            $userinfo['department_name'] = $dep_data[$depid]['department_name'];
            $userinfo['bu_name'] = $business[$dep_data[$depid]['bu_id']]['bu_name'];
            $userinfo['city_id'] = $dep_data[$depid]['city_id'];
            $userinfo['city_name'] = $citylist[$dep_data[$depid]['city_id']];
            $data[$job_id] = $userinfo;
        }
        return $data;
    }
    private $user_ids=array();
    private $job_ids = array();
    /**
     *
     */
    public function set_user_ids($uids){
        $this->user_ids = $uids;
    }
    /**
     *
     * @param int $jobids
     */
    public function set_job_id($jobid){
        $this->job_ids[] = $jobid;
    }
    /**
     * 搜索在职用户,如果部门和关键词全为空,所有员工
     * @param array $where
     * @param int $limit
     * @param int $offset
     * @return multitype:number multitype: |multitype:unknown
     * @author andycao
     */
    public function get_work_user_list($where, $limit, $offset){
        $data = array(
            'count'=>0,
            'datalist'=>array()
                );
        $search_type = $where['search_type'];
        if(empty($search_type)){
            return $data;
        }
        $allow = APF::get_instance()->get_config('hr_auth','auth');
        $dep_where = array(
                'is_deleted'=>1
                );
        if(!in_array($_SESSION['user_code'], $allow) && !empty($where['role_list'])){
            $dep_where["city_id"]=$where['role_list'];
        }

        //如果带搜索条件，先搜索部门 每次都查询所有部门信息后面用来格式化员工信息
        $dep_list = $this->get_dep_bll()->find_where($dep_where);
        $dep_ids = array();
        if(isset($where['city_id']) || isset($where['bu_id']) || isset($where['function_id'])){
            foreach($dep_list as $val){
                if(isset($where['city_id']) && $where['city_id'] !=$val['city_id']){
                    continue;
                }
                if(isset($where['bu_id']) && $where['bu_id'] !=$val['bu_id']){
                    continue;
                }
                if(isset($where['function_id']) && $where['function_id'] !=$val['function_id']){
                    continue;
                }
                $dep_ids[]=$val['department_id'];
            }
            if(empty($dep_ids)){
                return $data;
            }
        }else{
            $dep_ids=array_keys($dep_list);
        }

        $job_ids = array();
        $job_list = array();
        $kw = trim($where['kw']);

        if($search_type==3){
            if(!empty($dep_ids)){
                $job_where["department_id IN(".implode(',',$dep_ids).") AND 1=?"]=1;
            }
            if(!empty($kw)){
                $job_where["position_name LIKE '%{$kw}%' AND 1=?"]=1;
            }
            //需要所有的职位
            $job_where["is_deleted"]=1;
            $jobdata = $this->get_job_bll()->find_where($job_where);
            if(empty($jobdata)){//没有职位肯定没有人
                return $data;
            }
            foreach($jobdata as $val){
                if(!empty($this->job_ids) && in_array($val['job_id'],$this->job_ids)){//外部按职位搜索
                    $job_ids = array($val['job_id']);
                    $job_list[$val['job_id']] = $val;
                    break;
                }else{
                    $job_ids[]=$val['job_id'];
                    $job_list[$val['job_id']] = $val;
                }
            }
        }else{
            //获取部门职位
            if(!empty($dep_ids)){
                $job_list = $this->get_job_bll()->get_job_by_dep($dep_ids);
                if(!empty($job_list)){
                    $job_ids = array_keys($job_list);
                }
            }
        }

        $uids = array();
        $user_job_ids = array();//员工对应职位关系
        //搜索职位 人
        if(!empty($job_ids)){
            $user_job = $this->get_relation_bll()->get_job_user_list($job_ids);
            if(empty($user_job)){
                return $data;
            }
            foreach($user_job as $val){
                $user_job_ids[$val['user_id']] = $val['job_id'];
            }
            $uids = array_keys($user_job_ids);
        }

        //先搜索 人
        $_where = array();
        if(!empty($this->user_ids)){
            $uids = $this->user_ids;
            $tmp_job_list = $this->get_relation_bll()->get_job_by_uids($uids);
            foreach($tmp_job_list as $val){
                $user_job_ids[$val['user_id']] = $val['job_id'];
            }
            $job_ids = array_values($user_job_ids);
            $job_list = $this->get_job_bll()->find_by_job_ids($job_ids);
        }
        //如果没有先查询职位，说明是直接搜索人名或员工号的
        if(!empty($uids)){
            $_where["user_id IN(".implode(',',$uids).") AND 1=?"] = 1;
        }
        //按工作城市查询
        if(isset($where['city_id'])){
            $_where["work_city_id"] = $where['city_id'];
        }

        if(!empty($kw)){
            if($search_type==1){
                $_where["user_name LIKE '%{$kw}%' AND 1=?"] = 1;
            }elseif($search_type==2){
                $_where["user_code LIKE '%{$kw}%' AND 1=?"] = 1;
            }
        }

        $_where["job_status"] = 1;
        $count = $this->get_user_dao()->find_count($_where);
        if(empty($count)){
            return $data;
        }
        $datalist = $this->get_user_dao()->find_short_assoc($_where," user_code DESC ",$limit,$offset);
        //根据员人id得到员人的职位,做出数据job_list

        if(empty($uids)){
            $ck_uids=array_keys($datalist);
            $tmp_job_list = $this->get_relation_bll()->get_job_by_uids($ck_uids);
            foreach($tmp_job_list as $val){
                $user_job_ids[$val['user_id']] = $val['job_id'];
            }
            $job_ids = array_values($user_job_ids);
            $job_list = $this->get_job_bll()->find_by_job_ids($job_ids);
        }

        //格式化数据 任职职位 任职部门
        foreach($datalist as $k=>$val){
            $user_job_id =$user_job_ids[$val['user_id']];
            $dep_id = $job_list[$user_job_id]['department_id'];
            $val['job_info'] = $job_list[$user_job_id];
            $val['dep_info'] = $dep_list[$dep_id];
            $datalist[$k] = $val;
        }

        $data = array(
            'count'=>$count,
            'datalist'=>$datalist
                );
        return $data;
    }
    /**
     * 根据条件获取用户信息
     *
     * @param array $where
     * @return data
     */
    public function get_user_by_condition($where) {
        $result = array();
        if(empty($where)) {
            return $result;
        }
        $result = $this->get_user_dao()->find_assoc($where,'user_id asc',0,0);
        return $result;
    }
    /**
     * 获取所有员工id
     */
    public function get_all_user_ids(){
        $result = $this->get_user_dao()->find_short_assoc(array(), null, 0, 0,array('user_id','job_status'));
        return $result;
    }
    /**
     * 获取用户工作职位信息
     */
    public function search_user_work_info($user_ids, $is_parttime=1, $is_deleted=1){
        $data = array();
        if(empty($user_ids)){
            return $data;
        }
        $_work_where = array(
             "user_id IN(".implode(',',$user_ids).") AND 1=?"=>1,
             "is_parttime"=>$is_parttime,
                );
        if($is_deleted == 1 || $is_deleted == 2){
            $_work_where['is_deleted'] = $is_deleted;
        }
        $user_work = $this->get_work_dao()->find($_work_where,' work_id DESC ',0);

        if(empty($user_work)){
            return $data;
        }

        $tmp_jobids = array();
        //一个人可能任职多个职位,work_id倒序就是最后一个职位
        foreach($user_work as $val){
            if(!isset($jobids[$val['user_id']])){
                $tmp_jobids[$val['user_id']] = $val['job_id'];
            }
        }
        $jobids = array_values($tmp_jobids);
        $jobids = array_unique($jobids);
        $job_bll = new Bll_Hr_Job();
        $job_info = $job_bll->find_by_job_ids($jobids,3);//所有职位

        $tmp_depids = array();
        foreach($job_info as $val){
            $tmp_depids[$val['job_id']] = $val['department_id'];
        }
        $depids = array_values($tmp_depids);
        $depids = array_unique($depids);

        $dep_bll = new Bll_Hr_Department();
        $dep_data = $dep_bll->get_department_by_ids($depids,3);

        $bll = new Bll_Hr_Property();
        $business = $bll->get_business_list();
        $citylist = $bll->get_sort_city_list();

        $parent_uids = array();
        foreach($tmp_jobids as $uid=>$jobid){
            //取上级
            $depinfo = $dep_data[$tmp_depids[$jobid]];
            $parent = $this->get_relation_bll()->get_user_parent($uid,$jobid);
            if(!empty($parent)){
                $parent_uids[$uid] = $parent[0]['parent_id'];
            }
            $depid= $tmp_depids[$jobid];
            $data[$uid]['user_id'] = $uid;
            $data[$uid]['job_id'] = $jobid;
            $data[$uid]['position_name'] = $job_info[$jobid]['position_name'];
            $data[$uid]['department_id'] = $depid;
            $data[$uid]['department_name'] = $dep_data[$depid]['department_name'];
            $data[$uid]['bu_name'] = $business[$dep_data[$depid]['bu_id']]['bu_name'];
            $data[$uid]['city_name'] = $citylist[$dep_data[$depid]['city_id']];
            $data[$uid]['superior_code'] = '';
        }

        if(!empty($parent_uids)){
            $p_uids = array_values($parent_uids);
            $p_uids = array_unique($p_uids);
            $parent_data = $this->get_user_by_ids($p_uids);
            foreach($data as $uid=>$val){
                $parent_uid = $parent_uids[$uid];
                $data[$uid]['superior_code']=$parent_data[$parent_uid]['user_code'];
                $data[$uid]['superior_user_id']=$parent_data[$parent_uid]['user_id'];
                $data[$uid]['superior_user_name']=$parent_data[$parent_uid]['user_name'];
             }
        }
        return $data;
    }
    /**
     * 生成员工工号
     *
     * 规则：
     * 1、正式员工和试用期员工：城市拼音开头字母简写+年份+号码
     * 2、实习员工：城市拼音开头字母简写+S+号码
     * 3、兼职人员、临时工：城市拼音开头字母简写+P+号码
     * @param  $user_type int
     * @param  $city_id int
     * @return code string| error :0
     */
    public function get_user_code($user_type,$city_id,$only_get = true){
        if(!$user_type || !$city_id){
            return 0;
        }
        #1.获取城市缩写
        $city_short = '';
        $temp_citys = Const_BaseInfo::get_city_list();
        $city_short = $temp_citys[$city_id]['code_prefix'];
        $city_short = strtoupper($city_short);

        #2.获取年份字母
        $year_word = '';
        #2.获取当前最大用户编号
        $user_types = array();
        //正式员工和试用期员工  “A”代表2007年，“B”代表2008年，以此类推
        if($user_type == Const_User::USER_TYPE_FORMAL_STAFF || $user_type == Const_User::USER_TYPE_TRY_STAFF){
            $year_word = $this->_get_yesr_to_word();
        }elseif($user_type == Const_User::USER_TYPE_PRACTICE_STAFF){//实习员工
            $year_word = 'S';
        }elseif($user_type == Const_User::USER_TYPE_PART_TIME_STAFF || $user_type == Const_User::USER_TYPE_TMPORARY_STAFF){//兼职人员、临时工
            $year_word = 'P';
        }else{//error
            return 0;
        }
        $now_max_code_num = $this->get_user_nexr_code_num($year_word,$only_get);
        if($city_short && $year_word && $now_max_code_num){
            $code = $city_short.$year_word.$now_max_code_num;
            return $code;
        }else{
            return 0;
        }
    }
    private $max_code_num;//编码数字
    /**
     * 测试专用
     * 设置用户工号
     * @param array $user_type_array
     * @param int $max_code_num
     */
    public function set_user_max_code($user_type_array,$max_code){
        $key = $user_type_array;
        $this->max_code_num[$key] = $max_code;
    }
    //获取最大用户工号
    private function get_user_nexr_code_num($user_type_array,$only_get=true){
        $key = $user_type_array;
        if($this->max_code_num && isset($this->max_code_num[$key])){

        }else{
            $code = $this->get_user_dao()->get_user_max_code($user_type_array,$only_get);
            $this->max_code_num[$key] = $code;
        }
        return $this->max_code_num[$key];
    }
    /**
     * 员工离职
     * @param unknown_type $user_id
     * @param unknown_type $leaving_date 办理离职日期 20120202
     * @param unknown_type $last_date 最后工作日 20120202
     * @param int $leaving_type 离职类型
     * @param unknown_type $reason_id 离职原因
     * @param unknown_type $other_reason
     */
    public function update_leving_status($user_id, $leaving_date, $last_date, $leaving_type, $reason_id, $other_reason = ''){
        if(empty($user_id) || empty($leaving_date) || empty($last_date) || empty($leaving_type)){
            return false;
        }
        $data = array(
            'job_status' => 2,
            'leaving_date' => intval($leaving_date),
            'last_date' => intval($last_date),
            'leaving_type' => intval($leaving_type),
            'reason_id' => intval($reason_id),
            'other_reason' => $other_reason
        );
        return $this->get_user_dao()->update_by_id($user_id, $data);
    }
    /**
     * 获取员工入职记录
     */
    public function get_join_log($user_id){
        $result = $this->get_join_log_dao()->find(array('user_id'=>$user_id));
        return $result;
    }
    /**
     * 添加续约记录
     */
    public function add_join_log($temp_data){
        $data['created'] = intval(time());
        $data['last_update']=date('Y-m-d H:i:s');
        $data['join_start'] = $temp_data['join_start'];
        $data['join_end'] = $temp_data['join_end'];
        $data['join_keep'] = $temp_data['join_keep'];
        $data['user_id'] = $temp_data['user_id'];
        return $this->get_join_log_dao()->insert($data);
    }
    /**
     * 批量修改员工城市id
     * @param unknown_type $uids
     * @param unknown_type $city_id
     * @return boolean
     */
    public function update_work_city_id($uids, $city_id){
        $status = false;
        if(empty($uids)){
            return $status;
        }
        $where = array(
            "user_id IN(".implode(',',$uids).") AND 1=?"=>1
                );
        $status = $this->get_user_dao()->update(array('work_city_id'=>$city_id), $where);
        return $status;
    }
    /**
     * 检测邮箱是否被使用
     * @param string $email
     * @return boolean
     */
    public function check_email_is_used($email){
        $result = false;
        if(!$email){
            return $result;
        }
        $res = $this->get_user_dao()->find(array('office_mail'=>$email));
        if(!empty($res)){
            $result = true;
        }
        return $result;
    }
    /**
     * 验证用户身份证号
     * @param unknown_type $id_card
     * @return boolean
     */
    public function check_idcard_is_used($id_card,$id_type){
        if(!$id_card || !$id_type){
            return 0;
        }
        $res = $this->get_user_dao()->find(array('identity_code'=>$id_card,'identity_type'=>$id_type));
        if(!empty($res)){
            $result = 0;
        }
        return $res[0]['user_id'];
    }
    /**
     * 检测是否有重名用户
     * @param unknown_type $real_name
     * @return boolean
     */
    public function check_user_name_only($user_name){
        $result = false;
        if(!$user_name){
            return $result;
        }
        $res = $this->get_user_dao()->find(array('user_name'=>$user_name));
        if(!empty($res)){
            $result = true;
        }
        return $result;
    }
    public function get_all_mail_account(){
        $where = array();
        $fields =  array('domain_account','crm_account','office_mail');
        return $this->get_user_dao()->find_short($where, null,0,0,$fields);
    }
    /**
     * “A”代表2007年，“B”代表2008年，以此类推
     * 超过‘Z’对应的年份之后，没做处理
     */
    private function _get_yesr_to_word(){
        $now_year = intval(date('Y'));
        $base_year = 2007;
        $i = $now_year - $base_year;
        $words = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','Q','R','T','U','V','W','X','Y','Z');
        return $words[$i];
    }
    public function save_log($data){
        if(empty($data)){
            return false;
        }
        $data = json_encode($data);
        $in_data = array(
            'user_id'=>$_SESSION['user_id'],
            'content'=>$data,
            'opt_url'=>$_SERVER['HTTP_REFERER']
        );
        $this->get_adminlog_dao()->insert($in_data);
        return true;
    }
    //log
    private function get_adminlog_dao(){
        $dao = $this->get_dao('Dao_Hr_AdminLog');
        return $dao;
    }
    private function get_user_dao(){
        $dao = $this->get_dao('Dao_Hr_User');
        return $dao;
    }
    private function get_work_dao(){
        $dao = $this->get_dao('Dao_Hr_Work');
        return $dao;
    }
    private function get_join_log_dao(){
        $dao = $this->get_dao('Dao_Hr_JoinLog');
        return $dao;
    }
    private function get_report_user_dao(){
        $dao = $this->get_dao('Dao_Report_Report');
        return $dao;
    }
}
